package com.lyc.service.impl;

import com.lyc.DTO.UserDTO;
import com.lyc.mapper.UserMapper;
import com.lyc.pojo.User;
import com.lyc.service.UserService;
import com.lyc.utils.Md5Util;
import com.lyc.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    // 根据用户名查询用户
    @Override
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    // 注册
    @Override
    public void register(String username, String password) {
        // 对密码进行MD5加密
        String md5String = Md5Util.getMD5String(password);
        //添加
        userMapper.add(username,md5String);
    }

    // 更新用户信息
    @Override
    public void updateUserInfo(UserDTO userDTO) {
        // 将DTO对象转换为User对象
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userMapper.updateUserInfo(user);
    }

    // 修改密码
    @Override
    public void updatePassword(String newPassword) {
        //获取用户名
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        //对密码进行MD5加密
        String md5String = Md5Util.getMD5String(newPassword);
        userMapper.updatePassword(md5String, username);
    }


}
